//
//  PhotoViewController.m
//  BeanTrip
//
//  Created by dlios on 15-7-8.
//  Copyright (c) 2015年 PandaGroup. All rights reserved.
//

#import "PhotoViewController.h"
#define WIDTH ([UIScreen mainScreen].bounds.size.width/375)
#define HEIGHT ([UIScreen mainScreen].bounds.size.height/667)
#import "UIImageView+WebCache.h"
#import "daysModel.h"
#import "PhotoCell.h"


@interface PhotoViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>
@property(nonatomic, retain)UIImageView *myImageView;
@property(nonatomic, retain)UICollectionView *collectionView;
@property(nonatomic, retain)NSMutableArray *presentPicArr;
@end

@implementation PhotoViewController
- (void)dealloc
{
    [_photoImageArr release];
    [_titleName release];
    [_myImageView release];
    [_collectionView release];
    [_presentPicArr release];
    [super dealloc];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
   // self.view.backgroundColor = [UIColor whiteColor];
    
     self.view.backgroundColor=[UIColor colorWithRed:68/255.0 green:176/255.0 blue:193/255.0 alpha:1.0];
    
    
    
    
    //标题
    self.title = self.titleName;
    
    //开包存储model
    self.presentPicArr = [NSMutableArray array];
    for (NSDictionary *wayDic in self.photoImageArr) {
        NSMutableArray *arrar = wayDic[@"waypoints"];
        for (daysModel *daysM in arrar) {
            if (![daysM.photo isEqualToString:@""]) {
               [self.presentPicArr addObject:daysM];
            }
            
        }
    }
    
    
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake(WIDTH*375, HEIGHT*667 - 64 - 49);
    flowLayout.sectionInset  = UIEdgeInsetsMake(0, 0, 0, 0);
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    flowLayout.minimumInteritemSpacing = 0;
    flowLayout.minimumLineSpacing = 0;
    
    
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, WIDTH*375, HEIGHT*667 - 64 - 49) collectionViewLayout:flowLayout];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.view addSubview:self.collectionView];
    [_collectionView release];
    self.collectionView.pagingEnabled = YES;
    
    [self.collectionView registerClass:[PhotoCell class] forCellWithReuseIdentifier:@"reuse"];
    

}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.presentPicArr.count;

}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    PhotoCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    daysModel *daysM = self.presentPicArr[indexPath.row];
    [cell.myImageView sd_setImageWithURL:[NSURL URLWithString:daysM.photo] placeholderImage:[UIImage imageNamed:@"BeanTrip"]];
    cell.myTextLabel.text = daysM.text;
    cell.myTimeLabel.text = daysM.local_time;
    cell.dayTimeArrLabel.text = [NSString stringWithFormat:@"第%@天  %ld / %ld",daysM.day,indexPath.row + 1,self.presentPicArr.count];

    
    
    
    //计算文字的高
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:16*HEIGHT],NSFontAttributeName, nil];
    CGRect rect = [cell.myTextLabel.text boundingRectWithSize:CGSizeMake(cell.contentView.frame.size.width - 60*WIDTH, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    if (rect.size.height > 60*HEIGHT) {
        cell.myScrollView.contentSize = CGSizeMake(0, rect.size.height);
    }else{
    cell.myScrollView.contentSize = CGSizeMake(0, 60*HEIGHT);
    }
    cell.myTextLabel.frame = CGRectMake(30*WIDTH, 0, WIDTH * 375 - 60*WIDTH, rect.size.height);
    
    
    
    //计算图片高度
    NSDictionary *heightDic = (NSDictionary *)daysM.photo_info;
    NSString *heightPho = heightDic[@"h"];
    CGFloat heig = [heightPho integerValue];
    
    NSString *widthPho = heightDic[@"w"];
    CGFloat wid = [widthPho integerValue];
    
    CGFloat picHieght = 375*WIDTH * heig / wid;
    if (picHieght >(667*HEIGHT- 64-49)) {
        picHieght = 667*HEIGHT-64-49;
    }
    cell.myImageView.frame = CGRectMake(0, 0, 375*WIDTH, picHieght);
    cell.myImageView.center = cell.contentView.center;

    
    
    return cell;
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
